package com.firebird.app.kotlin.stock.ui.godeal.presenter

import com.firebird.app.kotlin.stock.App
import com.firebird.app.kotlin.stock.base.BasePresenter
import com.firebird.app.kotlin.stock.data.DetailsDealBean
import com.firebird.app.kotlin.stock.ui.godeal.contract.GoDealContract
import com.firebird.app.kotlin.stock.ui.godeal.model.GoDealModel
import com.firebird.app.kotlin.stock.utils.SharedPreferencesUtil
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class GoDealPresenter : BasePresenter<GoDealContract.View?>(), GoDealContract.Presenter {
    private val model: GoDealContract.Model
    override fun getData(body: RequestBody?) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getData(body)!!.enqueue(object : Callback<DetailsDealBean?> {
            override fun onResponse(call: Call<DetailsDealBean?>, response: Response<DetailsDealBean?>) {
                mView!!.hideLoading()
                mView!!.onSuccess(response.body())
                SharedPreferencesUtil.getInstance(App.context!!)!!.putSP("token", response.headers()["token"])
            }

            override fun onFailure(call: Call<DetailsDealBean?>, t: Throwable) {
                mView!!.hideLoading()
                mView!!.onError(t)
            }
        })
    }

    init {
        model = GoDealModel()
    }
}